package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    //添加用户
    @PostMapping //("{roleIds}")
    public Result handleCreate(@PathParam("roleIds") Integer[] roleIds, @RequestBody User user) {
        log.info("进入添加用户的方法了");
        try {
            //密码加密
            user.setPassword(MD5Utils.md5(user.getPassword()));
            //添加数据
            this.userService.add(roleIds, user);

            return new Result(true, MessageConstant.ADD_USER_SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_USER_FAIL);
    }


    //分页查询
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = this.userService.findPage(queryPageBean);
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS, pageResult);
            return result;
        } catch (Exception e) {
            log.error("分页查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }

    //根据id查询User
    @GetMapping("/{row.id}")
    public Result findUserById(@PathVariable("row.id") Integer userId) {
        try {
            User user = this.userService.findUserById(userId);
            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
            return result;
        } catch (Exception e) {
            log.error("根据userById查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }


    //根据id查询Roles       axios.get("/user/" + row.id + "/roleIds")
    @GetMapping("/{row.id}/roleIds")
    public Result findRolesById(@PathVariable("row.id") Integer userId) {
        try {
            ArrayList<Integer> roleIds = this.userService.findRolesById(userId);

            Result result = new Result(true, MessageConstant.QUERY_USER_SUCCESS, roleIds);
            return result;
        } catch (Exception e) {
            log.error("根据ids查询用户失败了", e);
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }

    //修改
    @PutMapping         //put http://localhost:9000/user?roleIds=%203,4
    public Result handleEdit(@PathParam("roleIds") Integer[] roleIds, @RequestBody User user) {

        try {
            //加密
            user.setPassword(MD5Utils.md5(user.getPassword()));
            this.userService.editUser(roleIds, user);
            Result result = new Result(true, MessageConstant.EDIT_USER_SUCCESS);
            return result;
        } catch (Exception e) {
            log.error("修改用户信息失败了", e);
        }
        return new Result(false, MessageConstant.EDIT_USER_FAIL);
    }


    //删除
    @DeleteMapping("/{row.id}")
    public Result deleteUser(@PathVariable("row.id") Integer userId) {
        try {
            this.userService.deleteUser(userId);
            Result result = new Result(true, MessageConstant.DELETE_USER_SUCCESS);
            return result;
        } catch (Exception e) {
            log.error("删除用户失败了，userid={}", userId);
        }
        return new Result(false, MessageConstant.DELETE_USER_FAIL);
    }


    /**
     //     * 获取当前登录用户的用户名
     //     *
     //     * @return
     //     * @throws Exception
     //     */
    @GetMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
   /* //动态菜单
    @GetMapping("/getUsername")
    public Result getUsername() throws Exception {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }*/
}











